package com.xingzhe.pengji.security.mapper.provider;

import java.util.Map;

import com.xingzhe.pengji.security.domain.User;
import com.xingzhe.pengji.security.sql.common.Enums;
import com.xingzhe.pengji.security.sql.common.SqlColumnsUtil;
import com.xingzhe.pengji.security.sql.common.SqlKeyWord;

/**
 * @Title ： 三江学院 毕业设计
 * @Description ：
 * @author ：LuFengLiang
 * @Company: IT行者工作室
 * @Copyright: Copyright (c) 2012-2-24
 * @version ： 1.0
 * @param <T>
 */
public class UserSqlProvider<T extends User> extends BaseSqlProvider<T>
{

    public String updateUser(T entity)
    {
        StringBuilder sb = new StringBuilder();

        sb.append(SqlColumnsUtil.USER_UPDATE_TOP_SQL);

        if ( null != entity.getName() )
        {
            sb = buildSelectStringForSet(sb, "name", entity.getName());
        }
        if ( null != entity.getPassword() )
        {
            sb = buildSelectStringForSet(sb, "password", entity.getPassword());
        }
        if ( null != entity.getEmail() )
        {
            sb = buildSelectStringForSet(sb, "email", entity.getEmail());
        }
        if ( 0 != entity.getEnabled() )
        {
            sb = buildSelectStringForSet(sb, "enabled", entity.getEnabled());
        }
        if ( 0 != entity.getIsSys() )
        {
            sb = buildSelectStringForSet(sb, "is_sys", entity.getIsSys());
        }
        if ( 0 != entity.getAccountNonExpired() )
        {
            sb = buildSelectStringForSet(sb, "account_non_expired", entity.getAccountNonExpired());
        }
        if ( 0 != entity.getAccountNonLocked() )
        {
            sb = buildSelectStringForSet(sb, "account_non_locked", entity.getAccountNonLocked());
        }
        if ( 0 != entity.getCredentialsNonExpired() )
        {
            sb = buildSelectStringForSet(sb, "credentials_non_expired", entity.getCredentialsNonExpired());
        }
        if ( 0 != entity.getDelFlag() )
        {
            sb = buildSelectStringForSet(sb, "del_flag", entity.getDelFlag());
        }

        if ( null != entity.getUpdateTime() )
        {
            sb = buildSelectStringForSet(sb, "update_time", entity.getUpdateTime());
        }
        if ( null != entity.getUpdateMan() )
        {
            sb = buildSelectStringForSet(sb, "update_man", entity.getUpdateMan());
        }
        sb.deleteCharAt(sb.lastIndexOf(","));
        sb.append(SqlKeyWord.KeyWord.SPACE.getValue());
        sb.append(SqlKeyWord.KeyWord.WHERE.getValue());
        sb.append(SqlKeyWord.KeyWord.SPACE.getValue());
        sb = buildSelectStringForSet(sb, "id", entity.getId());
        sb.deleteCharAt(sb.lastIndexOf(","));
        return sb.toString();
    }

    @Override
    public StringBuilder dynamicWhereSql(StringBuilder sb, Map<String, Object> map)
    {
        sb.append(" ");

        int id = (Integer) map.get("id");
        if ( id != 0 )
        {
            sb.append("and id =").append(id).append(" ");
        }

        String account = (String) map.get("account");
        if ( null != account )
        {
            sb.append("and account = ").append("'").append(account).append("'").append(" ");
        }

        int enabled = (Integer) map.get("enabled");
        if ( enabled != 0 )
        {
            sb.append("and enabled =").append(enabled).append(" ");
        }

        int isSys = (Integer) map.get("isSys");
        if ( isSys != 0 )
        {
            sb.append("and is_sys =").append(isSys).append(" ");
        }

        String name = (String) map.get("name");
        if ( null != name )
        {
            sb.append("and name like ").append("'%").append(name).append("%'").append(" ");
        }

        String email = (String) map.get("email");
        if ( null != email )
        {
            sb.append("and email like '%").append(email).append("%'").append(" ");
        }

        int accountNonExpired = (Integer) map.get("accountNonExpired");
        if ( accountNonExpired != 0 )
        {
            sb.append("and account_non_expired =").append(accountNonExpired).append(" ");
        }

        int accountNonLocked = (Integer) map.get("accountNonLocked");
        if ( accountNonLocked != 0 )
        {
            sb.append("and account_non_locked =").append(accountNonLocked).append(" ");
        }

        int credentialsNonExpired = (Integer) map.get("credentialsNonExpired");
        if ( credentialsNonExpired != 0 )
        {
            sb.append("and credentials_non_expired =").append(credentialsNonExpired).append(" ");
        }

        return sb;
    }

    
    public String getAllAuthoritiesByUserAccount(String userAccount)
    {
    	StringBuilder sb=new StringBuilder();
    	sb.append("SELECT  DISTINCT sr.url FROM sao_resources sr ");
    	sb.append("JOIN  sao_authorites_resources sar ON  sr.id=sar.resource_id");
    	sb.append("JOIN sao_authorites sa ON sa.id=sar.authorite_id ");
    	sb.append("JOIN sao_groups_autorites sga ON sga.autorite_id = sa.id ");
    	sb.append("JOIN sao_groups sg ON sg.id=sga.group_id ");
    	sb.append("JOIN sao_users_groups sug ON sug.group_id=sg.id ");
    	sb.append("JOIN sao_users su ON su.id=sug.user_id ");
    	sb.append("WHERE su.account='");
    	sb.append(userAccount);
    	sb.append("'");
    	return sb.toString();
    }
    /**
     * 根据用户的查询所有的满足条件的用户
     * @param  map 键值对
     * @return
     */
    public String getUser(Map<String, Object> map)
    {
        StringBuilder sb = new StringBuilder();
        sb = getSelectString(sb);
        return dynamicWhereSql(sb, map).toString();
    }

    @Override
    public String getCommonColumnsString()
    {
        return SqlColumnsUtil.USER_COMMON_COLUMNS;
    }

    @Override
    public StringBuilder getFromTableString(StringBuilder sb)
    {
        return buildString(sb, Enums.TableName.USER.getDesc());
    }
}
